package com.ruicar.afs.cloud.product.controller;

import com.ruicar.afs.cloud.afscase.dispatch.config.CaseApiConfig;
import com.ruicar.afs.cloud.afscase.dispatch.contract.CaseRuleAtomFeign;
import com.ruicar.afs.cloud.afscase.dispatch.dto.CaseRuleAtomDto;
import com.ruicar.afs.cloud.channel.config.ChannelApiConfig;
import com.ruicar.afs.cloud.channel.dto.RuleAtomDto;
import com.ruicar.afs.cloud.channel.feign.ChannelApiFeign;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yaorui.zhang
 * @date 2020年5月11日19:40:40
 */
@RestController
@AllArgsConstructor
@RequestMapping("/ruleAtomQuery")
@Api(value = "ruleAtomQuery", description = "规则原子查询")
public class RuleAtomController {

    //渠道
    private final ChannelApiFeign channelApiFeign;
    private final ChannelApiConfig channelApiConfig;

    //案件
    private final CaseApiConfig caseApiConfig;
    private final CaseRuleAtomFeign caseRuleAtomFeign;

    @PostMapping(value = "/getBrandListByRule")
    @ApiOperation(value = "查询品牌列表")
    public IResponse<Object> getBrandListByRule(@RequestBody RuleAtomDto ruleAtomDto) {
        IResponse iResponse = channelApiFeign.getBrandListByRule(ruleAtomDto, this.makeChannelHeader());
        return new IResponse<Object>().setData(iResponse.getData());
    }

    @PostMapping(value = "/getModelListByRule")
    @ApiOperation(value = "查询车型列表")
    public IResponse<Object> getModelListByRule(@RequestBody RuleAtomDto ruleAtomDto) {
        IResponse iResponse = channelApiFeign.getModelListByRule(ruleAtomDto, this.makeChannelHeader());

        return new IResponse<Object>().setData(iResponse.getData());
    }

    @PostMapping(value = "/getStyleListByRule")
    @ApiOperation(value = "查询款式列表")
    public IResponse<Object> getStyleListByRule(@RequestBody RuleAtomDto ruleAtomDto) {
        IResponse iResponse = channelApiFeign.getStyleListByRule(ruleAtomDto, this.makeChannelHeader());

        return new IResponse<Object>().setData(iResponse.getData());
    }

    @PostMapping(value = "/getAllCarDealers")
    @ApiOperation(value = "查询车商列表")
    public IResponse<Object> getAllCarDealers(@RequestBody CaseRuleAtomDto caseRuleAtomDto) {
        IResponse iResponse = caseRuleAtomFeign.getAllCarDealers(caseRuleAtomDto, this.makeCaseHeader());
        return new IResponse<Object>().setData(iResponse.getData());
    }

    @PostMapping(value = "/getAllChannelBaseInfo")
    @ApiOperation(value = "查询合作商列表")
    public IResponse<Object> getAllChannelBaseInfo(@RequestBody CaseRuleAtomDto caseRuleAtomDto) {
        IResponse iResponse = caseRuleAtomFeign.getAllCaseBaseInfo(caseRuleAtomDto, this.makeCaseHeader());
        return new IResponse<Object>().setData(iResponse.getData());
    }

    /**
     * 获取进件请求头
     * @return
     */
    public Map makeChannelHeader() {
        Map<String, String> headers = new HashMap<>();
        headers.put("clientId", channelApiConfig.getChannelClientId());
        headers.put("clientSecret", channelApiConfig.getChannelClientSecret());
        return headers;
    }

    /**
     * 获取进件请求头
     * @return
     */
    public Map makeCaseHeader() {
        Map<String, String> headers = new HashMap<>();
        headers.put("clientId", caseApiConfig.getCaseClientId());
        headers.put("clientSecret", caseApiConfig.getCaseClientSecret());
        return headers;
    }

}
